﻿
@{
    ViewBag.Title = "库存结转";
    Layout = "~/Views/Shared/_Index.cshtml";
}

<form id="form1">
    <div class="panel panel-default">
        <div class="panel-heading">
            结转
        </div>
        <div>
            <table class="form">
                <tr>
                    <th class="formTitle">历史结转：</th>
                    <td class="formValue">
                        <select id="lsjz" class="form-control"></select>
                    </td>
                    <th class="formTitle">输入码：</th>
                    <td class="formValue">
                        <input id="txtSRM" type="text" class="form-control" placeholder="拼音/药品名/药品代码" />
                    </td>
                    <td class="formValue" colspan="3">
                        <input type="button" id="btn_search" class="btn btn-primary" value="查询" style="margin-left:10px;" />
                    </td>
                </tr>
            </table>
        </div>
    </div>
    <div class="gridPanel">
        <table id="gridDrugsList"></table>
        <div id="gridPager" rel="form1"></div>
    </div>
    @Html.Partial("_BottomButtonsView", new Newtouch.HIS.Web.Core.Models.BottomButtonViewModel
{
    ShowKeyList = new[] { 8 },
    F8Text = "结转当前"
})
</form>
<script>
    $(function () {
        //浏览器窗口调整大小时重新加载jqGrid的宽
        $(window).resize(function () {
            window.initLayout("MyTabGrid");
        });
        GetlsjzSel();//获取历史结转时间
        gridDrugsList();//药品信息
    });

    //回车事件绑定
    $(document).keyup(function (event) {
        if (event.keyCode === 13) {
            $("#btn_search").trigger("click");
        }
    });

    //获取历史结转时间
    function GetlsjzSel() {
        $("#lsjz").html("");
        $("#lsjz").bindSelect({
            url: "/DrugStorage/GetlsjzDateTime",
            id: "key",
            text: "value"
        });
    }

    //执行结转当前
    function newtouch_event_f8() {
        //获取最后一次结转时间
        var lastCarryTime;
        $.najax({
            type: "Get",
            url: "/DrugStorage/QueryLastCarryTime?r=" + Math.random(),
            dataType: "json",
            success: function (ajaxresp) {
                if (ajaxresp != null) {
                    if (ajaxresp.state === "success") {
                        lastCarryTime = ajaxresp.data;
                        var msg = ajaxresp.data === "" ? "您确定结转吗？" : ("最近一次结转时间为：" + lastCarryTime + " ,您确定结转吗？")
                        $.modalConfirm(msg,
                            function (flag) {
                                if (flag) {
                                    $.najax({
                                        url: "/DrugStorage/CarryOverMedicine",
                                        type: "POST",
                                        dataType: "json",
                                        success: function (ajaxresp) {
                                            if (ajaxresp.state === 'success') {
                                                $.modalAlert("结转成功", 'success');
                                                GetlsjzSel();//刷新结转日期
                                                $("#btn_search").trigger('click');
                                            }
                                            else {
                                                $.newtouchAlert(ajaxresp);
                                            }
                                        }
                                    });

                                } else {
                                    return false;
                                }
                            }, true);
                    } else {
                        $.modalAlert(ajaxresp.message, "error");
                    }
                } else {
                    $.modalAlert("取上一次结转时间失败", "error");
                }
            }
        });
    }

    //药品信息
    function gridDrugsList() {
        var captionCon = "合计&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span id='yeCon'>当前页 批价总额：" + "" + "元&nbsp;&nbsp;&nbsp;&nbsp;零售总额：" + "" + "元</span>";
        var $gridDrugsList = $("#gridDrugsList");
        $gridDrugsList.dataGrid({
            postData: getSearchPostData(),
            height: $(window).height() - 230,
            url: "/DrugStorage/SelectCarryDownMedicineList",
            caption: captionCon,
            colModel: [
                { label: '药品名称', name: 'ypmc', width: 180, align: 'left' },
                { label: '药品代码', name: 'ypdm', width: 65, align: 'left' },
                { label: '规格', name: 'gg', width: 70, align: 'left' },
                { label: '结转数量', name: 'jzslstr', width: 60, align: 'right' },
                { label: '单位', name: 'dw', width: 50, align: 'center' },
                { label: '批次', name: 'pc', width: 110, align: 'left' },
                { label: '生产厂家', name: 'sccj', width: 150, align: 'left' },
                {
                    label: '批发价', name: 'pfj', width: 70, align: 'right', formatter: function (cellvalue) {
                        return cellvalue ? parseFloat(cellvalue).toFixed(4) : "0";
                    }
                },
                {
                    label: '零售价', name: 'lsj', width: 70, align: 'right', formatter: function (cellvalue) {
                        return cellvalue ? parseFloat(cellvalue).toFixed(4) : "0";
                    }
                },
                {
                    label: '批发总额', name: 'pfze', width: 90, align: 'right', formatter: function (cellvalue) {
                        return cellvalue ? parseFloat(cellvalue).toFixed(2) : "0";
                    }
                },
                {
                    label: '零售总额', name: 'lsze', width: 90, align: 'right', formatter: function (cellvalue) {
                        return cellvalue ? parseFloat(cellvalue).toFixed(2) : "0";
                    }
                },
                { label: '结转数量', name: 'jzsl', hidden: true }
            ],
            multiselect: false,
            pager: "#gridPager",
            sortname: 'CreateTime desc',
            viewrecords: true,
            loadComplete: function (data) {
                ComputePjzeAndLsze();//计算标题栏中的批价总额和零售总额
            }
        });

        //查询
        $("#btn_search").click(function () {
            $gridDrugsList.jqGrid('setGridParam', {
                postData: getSearchPostData()
            }).trigger('reloadGrid');
        });
    }

    //查询条件
    function getSearchPostData() {
        var jzsj = $("#lsjz").find("option:selected").text();
        var keyWork = $("#txtSRM").val();
        return { jzsj: jzsj, keyWork: keyWork };
    };

    //计算标题栏中的批价总额和零售总额
    function ComputePjzeAndLsze() {
        var titlePjze = 0;
        var titleLsze = 0;
        var count = $("#gridDrugsList").getGridParam('records');
        if (count > 0) {
            var rowIds = $("#gridDrugsList").getDataIDs();
            for (var i = 0; i < rowIds.length; i++) {
                var getRowPjze = $("#gridDrugsList").getRowData(rowIds[i]).pfze;
                var getRowLsze = $("#gridDrugsList").getRowData(rowIds[i]).lsze;
                titlePjze += parseFloat(getRowPjze);
                titleLsze += parseFloat(getRowLsze);
            }
            titlePjze = window.roundingBy4she6ru5chengshuang(titlePjze, 2);
            titleLsze = window.roundingBy4she6ru5chengshuang(titleLsze, 2);
            $("#yeCon").text("批价总额：" + titlePjze + "元;零价总额：" + titleLsze + "元");
            $.loading(false);
        }
    }
</script>